from flask import Flask,jsonify,request,Blueprint,session
from common.res import (getRes,getErrorRes,getSuccessRes,getDataRes)
from common.sqlInjectionSecurity import sqlEscape,sqlEscapeField
from DAL.admDAL import deviceDAL
from controller.logAttr import log
from controller.public_param import MODEL_DOWN_DIR,MODEL_UPLOAD_DIR
import os

adm_device = Blueprint('adm_device',__name__)

@adm_device.route("/adm_device/getlist",methods=['POST'])
def getlist():
    param = {
        'asset_type': request.form.get('asset_type') and sqlEscape(request.form.get('asset_type')) or "",
        'device_no': request.form.get('device_no') and sqlEscape(request.form.get('device_no')) or None,
        'brand_model': request.form.get('brand_model') and sqlEscape(request.form.get('brand_model')) or None,
        'worth': request.form.get('worth') and sqlEscape(request.form.get('worth')) or None,
        'user': request.form.get('user') and sqlEscape(request.form.get('user')) or None,
        'remark': request.form.get('remark') and sqlEscape(request.form.get('remark')) or None,
        'page': request.form.get('page') and sqlEscape(request.form.get('page')) or 1,
        'limit': request.form.get('limit') and sqlEscape(request.form.get('limit')) or 20,
        'filed': request.form.get('filed') and sqlEscape(request.form.get('filed')) or 'id',
        'sort': request.form.get('sort') and sqlEscape(request.form.get('sort')) or 'ascending'
          }
    dal = deviceDAL()
    data,count = dal.getList(**param)
    if data:
        res = getSuccessRes(data=data,count=count)
    else:
        res = getSuccessRes(data=[], count=count)
    return jsonify(res)


@adm_device.route("/adm_device/delete_by_id",methods=['POST'])
@log(api_url="/adm_device/delete_by_id")
def delete_by_id():
    param = {
        'id': request.form.get('id') or None
    }
    if not param['id']:
        res = getErrorRes("你输入的参数有错！")
        return jsonify(res)
    dal = deviceDAL()
    data = dal.deleteById(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)


@adm_device.route("/adm_device/notExistAdd",methods=['POST'])
@log(api_url="/adm_device/notExistAdd")
def add():
    param = {
        'asset_type': request.form.get('asset_type') and sqlEscape(request.form.get('asset_type')) or "",
        'device_no': request.form.get('device_no') and sqlEscape(request.form.get('device_no')) or None,
        'brand_model': request.form.get('brand_model') and sqlEscape(request.form.get('brand_model')) or None,
        'worth': request.form.get('worth') and sqlEscape(request.form.get('worth')) or None,
        'user': request.form.get('user') and sqlEscape(request.form.get('user')) or None,
        'remark': request.form.get('remark') and sqlEscape(request.form.get('remark')) or ''
    }
    if not param['device_no']:
        res = getErrorRes("设备号必须填写！")
        return jsonify(res)
    dal = deviceDAL()
    data = dal.notExistAdd(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)


@adm_device.route("/adm_device/update",methods=['POST'])
@log(api_url="/adm_device/update")
def update():
    param = {
        'id': request.form.get('id') or None,
        'asset_type': request.form.get('asset_type') and sqlEscape(request.form.get('asset_type')) or "",
        'device_no': request.form.get('device_no') and sqlEscape(request.form.get('device_no')) or None,
        'brand_model': request.form.get('brand_model') and sqlEscape(request.form.get('brand_model')) or None,
        'worth': request.form.get('worth') and sqlEscape(request.form.get('worth')) or None,
        'user': request.form.get('user') and sqlEscape(request.form.get('user')) or None,
        'remark': request.form.get('remark') and sqlEscape(request.form.get('remark')) or ''
    }
    if not param['id']:
        res = getErrorRes("参数id必须填写！")
        return jsonify(res)
    if not param['device_no']:
        res = getErrorRes("设备号必须填写！")
        return jsonify(res)
    dal = deviceDAL()
    data = dal.update(**param)
    res = getSuccessRes(data=data)
    return jsonify(res)

@adm_device.route("/adm_device/batch_processing/<path:filename>",methods=['POST'])
@log(api_url="/adm_device/batch_processing")
def batch_processing(filename):
    random_integer = request.form.get('random') or None
    if not random_integer:
        res = getErrorRes("操作有错！")
        return jsonify(res)
    url =os.path.join(MODEL_UPLOAD_DIR,random_integer+filename)
    dal = deviceDAL()
    data = dal.batch_processing(file_url=url)
    res = getSuccessRes(data=data)
    return jsonify(res)

